<?php
if (!defined('IN_RJ_ADMIN')) die("Error 404");
if ($level != 1) {
	echo "Bạn không có quyền vào trang này.";
	exit();
}
$edit_url = 'index.php?act=config&mode=edit';

$inp_arr = array(
		'root url'	=> array(
			'table'	=>	'rooturl',
			'name'	=>	$lang_acp['rooturl'],
			'type'	=>	'free'
		),
		'root path'	=> array(
			'table'	=>	'rootpath',
			'name'	=>	$lang_acp['rootpath'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'root img'	=> array(
			'table'	=>	'root_img',
			'name'	=>	$lang_acp['root_img'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'local img'	=> array(
			'table'	=>	'local_img',
			'name'	=>	$lang_acp['local_img'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'img thumb'	=> array(
			'table'	=>	'img_thumb',
			'name'	=>	$lang_acp['img_thumb'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'index title'	=> array(
			'table'	=>	'indextitle',
			'name'	=>	$lang_acp['indextitle'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'email'	=> array(
			'table'	=>	'email',
			'name'	=>	$lang_acp['email'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'local url'	=> array(
			'table'	=>	'local_url',
			'name'	=>	$lang_acp['local_url'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'total visit'	=> array(
			'table'	=>	'total_visit',
			'name'	=>	$lang_acp['total_visit'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'code down'	=> array(
			'table'	=>	'code_down',
			'name'	=>	$lang_acp['code_down'],
			'type'	=>	'free',
			'can_be_empty'	=>	true,
		),
		'public down'	=> array(
			'table'	=>	'public_down',
			'name'	=>	$lang_acp['public_down'],
			'type'	=>	'checkbox',
		),
		'default lang'	=> array(
			'table'	=>	'default_lang',
			'name'	=>	$lang_acp['default_lang'],
			'type'	=>	'checkbox',

		),
	);
##################################################
# ADD MEDIA CAT
##################################################
if ($mode == 'add') {
	acp_check_permission('add_cat');
	if ($_POST['submit']) {
		$error_arr = array();
		$error_arr = $form->checkForm($inp_arr);
		if (!$error_arr) {
			
			$sql = $form->createSQL(array('INSERT',$conf['prefix'].'cat'),$inp_arr);
			eval('$DB->query("'.$sql.'");');
			echo $lang_acp['added']." <meta http-equiv='refresh' content='0;url=$link'>";
			exit();
		}
	}
	$warn = $form->getWarnString($error_arr);

	$form->createForm($lang_acp['add_cat'],$inp_arr,$error_arr);
}
##################################################
# EDIT MEDIA CAT
##################################################
if ($mode == 'edit') {
	
	acp_check_permission('edit_cat');
	
	if ($cat_id) {
		if (!$_POST['submit']) {
			$q = $DB->query("SELECT * FROM ".$conf['prefix']."cat WHERE cat_id = '$cat_id'");
			$r = $DB->fetch_row($q);
			
			foreach ($inp_arr as $key=>$arr) $$key = $r[$arr['table']];
		}
		else {
			$error_arr = array();
			$error_arr = $form->checkForm($inp_arr);
			if (!$error_arr) {
				$sql = $form->createSQL(array('UPDATE',$conf['prefix'].'cat','cat_id','cat_id'),$inp_arr);
				eval('$DB->query("'.$sql.'");');
				echo $lang_acp['edited']." <meta http-equiv='refresh' content='0;url=".$edit_url."'>";
				exit();
			}
		}
		$warn = $form->getWarnString($error_arr);
		$form->createForm($lang_acp['edit_cat'],$inp_arr,$error_arr);
	}
	else {
		if ($_POST['sbm']) {
			$z = array_keys($_POST);
			$q = $DB->query("SELECT cat_id FROM ".$conf['prefix']."cat");
			for ($i=0;$i<$DB->num_rows($q);$i++) {
				$id = split('o',$z[$i]);
				$od = ${$z[$i]};
				$DB->query("UPDATE ".$conf['prefix']."cat SET cat_order = '$od' WHERE cat_id = '".$id[1]."'");
			}
		}
		echo "<script>function check_del(id) {".
		"if (confirm('{$lang_acp['ask_u_del']} ?')) location='?act=config&mode=del&cat_id='+id;".
		"return false;}</script>";
		echo "<table width=90% align=center cellpadding=2 cellspacing=0 class=border><form method=post>";
		echo "<tr><td align=center class=title width=5%>STT</td><td class=title style='border-right:0'>{$lang_acp['cat_name']}</td></tr>";
		$cat_query = $DB->query("SELECT * FROM ro_config ");
		while ($cat = $DB->fetch_row($cat_query)) {
			echo "<tr align=center><td colspan=2 class=cat_title>".$cat['cat_title']."</td></tr>";
			$iz = $cat['cat_order'];
			echo "<tr><td align=center class=fr><input onclick=this.select() type=text name='o".$cat['cat_id']."' value=$iz size=2 style='text-align:center'></td><td class=fr_2><a href=# onclick=check_del(".$cat['cat_id'].")>{$lang_acp['del']}</a> - <a href='$link&cat_id=".$cat['cat_id']."'><b>".$cat['cat_name']."</b></a></td></tr>";
			$sub_query = $DB->query("SELECT * FROM ".$conf['prefix']."cat WHERE sub_id = '".$cat['cat_id']."' ORDER BY cat_order ASC");
			if ($DB->num_rows($sub_query)) echo "<tr><td class=fr_2>&nbsp;</td><td class=fr><table width=100% cellpadding=2 cellspacing=0 class=border>";
			while ($sub = $DB->fetch_row($sub_query)) {
				$s_o = $sub['cat_order'];
				echo "<tr><td align=center class=fr width=5%><input onclick=this.select() type=text name='o".$sub['cat_id']."' value=$s_o size=2 style='text-align:center'></td><td class=fr_2><a href=# onclick=check_del(".$sub['cat_id'].")>{$lang_acp['del']}</a> - <a href='$link&cat_id=".$sub['cat_id']."'><b>".$sub['cat_name']."</b></a></td></tr>";
			}
			if ($DB->num_rows($sub_query)) echo "</table></td></tr>";
		}
		echo '<tr><td colspan="2" align="center"><input type="submit" name="sbm" class=submit value="'.$lang_acp['edit_order'].'"></td></tr>';
		echo '</form></table>';
	}
	
}
##################################################
# DELETE MEDIA CAT
##################################################
if ($mode == 'del') {
	acp_check_permission('del_cat');
	if ($cat_id) {
		if ($_POST['submit']) {
			$DB->query("DELETE FROM ".$conf['prefix']."cat WHERE cat_id = '".$cat_id."'");
			echo "Đã xóa xong <meta http-equiv='refresh' content='0;url=".$edit_url."'>";
			exit();
		}
		?>
		<form method="post"><?=$lang_acp['ask_u_del']?> ??????<br><input value="<?=$lang_acp['yes']?>" name=submit type=submit class=submit></form>
<?
	}
}
?>